package main

import (
	"encoding/binary"
	"net"
	"message"

	"github.com/golang/protobuf/proto"
	"github.com/name5566/leaf/log"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:3563")
	if err != nil {
		panic(err)
	}

	// 序列化实际的消息
	data := &message.C2S_Login{}
	data.Account = proto.String("Name")
	data.Password = proto.String("Psw")
	data.LoginType = message.LoginType_LOGIN_ACCOUNT.Enum()

	buffer, err := proto.Marshal(data)
	if err != nil {
		log.Error("error1")
		return
	}

	rpc := &message.RPCRequest{}
	rpc.MsgID = message.MSG_LOGIN.Enum()
	rpc.MsgIdentifier = proto.Int(0)
	rpc.Data = buffer

	bufferRPC, errRPC := proto.Marshal(rpc)
	if errRPC != nil {
		log.Error("error2")
		return
	}

	// len + data
	len := len(bufferRPC)
	m := make([]byte, 4+len)

	// 默认使用大端序
	binary.BigEndian.PutUint32(m, uint32(len))

	copy(m[4:], bufferRPC)

	// 发送消息
	conn.Write(m)
}
